package com.event;

import com.util.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;

/**
 * @author 大忽悠
 * @create 2022/9/15 20:59
 */
@Slf4j
public abstract class DomainEventListener implements ApplicationListener {

    @Override
    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        if(!(applicationEvent instanceof DomainEvent)){
            return;
        }
        DomainEvent domainEvent = (DomainEvent) applicationEvent;
        if (!supportDomainEvent(domainEvent)) {
            return;
        }
        log.info("当前时间: {},接收到的领域事件为: {}", TimeUtil.getCurTime(),domainEvent);
        if(domainEvent.getAsync()){
            log.info("当前领域事件采取异步处理");
        }
        doHandleDomainEvent(domainEvent);
    }

    /**
     * 是否支持当前领域事件
     */
    protected abstract boolean supportDomainEvent(DomainEvent domainEvent);

    /**
     * 处理接收到的领域事件
     */
    protected abstract void doHandleDomainEvent(DomainEvent domainEvent);
}
